package org.locker.device.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.CommonVo;
import org.locker.domain.bo.CommonBo;
import org.locker.service.ICommonService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 品牌信息
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/common")
public class CommonController extends BaseController {

    private final ICommonService commonService;

    /**
     * 查询品牌信息列表
     */
    @SaCheckPermission("device:common:list")
    @GetMapping("/list")
    public TableDataInfo<CommonVo> list(CommonBo bo, PageQuery pageQuery) {
        return commonService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出品牌信息列表
     */
    @SaCheckPermission("device:common:export")
    @Log(title = "品牌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommonBo bo, HttpServletResponse response) {
        List<CommonVo> list = commonService.queryList(bo);
        ExcelUtil.exportExcel(list, "品牌信息", CommonVo.class, response);
    }

    /**
     * 获取品牌信息详细信息
     *
     * @param commonId 主键
     */
    @SaCheckPermission("device:common:query")
    @GetMapping("/{commonId}")
    public R<CommonVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long commonId) {
        return R.ok(commonService.queryById(commonId));
    }

    /**
     * 新增品牌信息
     */
    @SaCheckPermission("device:common:add")
    @Log(title = "品牌信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommonBo bo) {
        return toAjax(commonService.insertByBo(bo));
    }

    /**
     * 修改品牌信息
     */
    @SaCheckPermission("device:common:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommonBo bo) {
        return toAjax(commonService.updateByBo(bo));
    }

    /**
     * 删除品牌信息
     *
     * @param commonIds 主键串
     */
    @SaCheckPermission("device:common:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commonIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] commonIds) {
        return toAjax(commonService.deleteWithValidByIds(List.of(commonIds), true));
    }
}
